package light.head.controller.oa;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.TaskDAO;
import light.head.dao.sys.MemorandumDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Memorandum;
import light.head.model.Task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = Url.C_MYTASK)
public class TaskOwnController extends SupporterController{
	
	private TaskDAO taskDAO;
	private MemorandumDAO maDAO;
	private OrganDAO organDAO;

	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.TASKOWN_INIT);
	}

	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.RU, Parm.START, Parm.END, Parm.TITLE, Parm.USER, Parm.OC);
		map.put(Parm.RU, cu_code(request).toString());
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (taskDAO.findByMap(organDAO.getSonCodeList(oc),map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		
		return new ModelAndView(Url.TASKOWN_LIST);
	}

	@RequestMapping(value = Url.DELETE)
	public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		
		
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		Task task = taskDAO.get(Integer.parseInt(code));
		if (task.getUser() != task.getRu()) {
			print(response, Msg.DELETED);
			return;
		}
		task.setDeleted(true);
		if (taskDAO.update(task) == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(null == cu_organCode(request)) {
			PageBean pb = createPageBean(request);
			request.setAttribute(Parm.PAGE_BEAN, pb);
			return new ModelAndView(Url.TASKOWN_INIT);
		}
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Task task = taskDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.TASK, task);
		}
		return new ModelAndView(Url.TASKOWN_EDIT);
	}
	
	@Transactional
	@RequestMapping(value = Url.SAVE)
	public void save(Task task, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (null == task.getCode()) {
			if(task.getTitle().equals("")) {
				print(response, Msg.NULL_TITLE);
				return;
			}if(task.getContent().equals("")) {
				print(response, Msg.NOT_FIND);
				return;
			}if(task.getStart() == null || task.getEnd() == null) {
				print(response, Msg.NULL_TIME);
				return;
			}
			task.setDeleted(false);
			task.setUser(cu_code(request));
			task.setDu(cu_code(request));
			task.setRu(cu_code(request));
			task.setOc(cu_organCode(request));
			result = taskDAO.add(task);
			Memorandum ma = new Memorandum();
			ma.setCu(cu_code(request));
			ma.setContent(task.getContent());
			ma.setDeleted(false);
			ma.setEnd(task.getEnd());
			ma.setOc(cu_organCode(request));
			ma.setStart(task.getStart());
			ma.setTitle(task.getTitle());
			maDAO.add(ma);
		} else {
			result = taskDAO.update(task);
		}
		
		if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}

	@Autowired
	public void setTaskDAO(TaskDAO taskDAO) {
		this.taskDAO = taskDAO;
	}
	@Autowired
	public void setMaDAO(MemorandumDAO maDAO) {
		this.maDAO = maDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}

